package com.zmn.mcc.services.impl.staff;

import com.zmn.common.utils.number.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 员工密码服务
 *
 * @author quanjic
 * @version v1.0
 * @since 2020/6/17 16:56
 **/
public class PasswordService {

    private int saltLength = 10;

    private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**
     * 生成密码
     *
     * @param password
     * @param salt
     * @return
     */
    public String generatePassword(String password, String salt) {
        if (StringUtils.isEmpty(password)) {
            return null;
        }
        return passwordEncoder.encode(password + ":" + salt);
    }

    /**
     * @return 生成10位随机数 salt
     */
    public String generateSalt() {
        return RandomUtil.getStringRandom(saltLength);
    }

}
